package trefliq.EJB.entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import javax.persistence.*;
import trefliq.EJB.sessionbeans.CartSession;

/**
 *
 * @author Brandon
 */
@Entity
@NamedQueries ({
    @NamedQuery(name="findAllCustomers", query="SELECT e FROM Customers e"),
    @NamedQuery(name="findCustomerById", query = "SELECT e FROM Customers e WHERE e.id = :id"),
    @NamedQuery(name="findCustomerByPersonId", query="SELECT e from Customers e where e.personId = :personId"),
})
public class Customers implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
//    @NotNull
    @Column(name = "ID", nullable = false)
//    @GeneratedValue(strategy=GenerationType.TABLE, generator="CUSTOMERS")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /* FK */ 
    private Long personId; 
    
    /* FK */ 
    private Long credentialId; 
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customerid", orphanRemoval = true)
    private Collection<Cart> cartCollection;
    
    public void Customers() { 
        
    }
    
    public void Customers(Long id) { 
        this.id = id; 
    }
    
    public void Customers(Long personId, Long credentialId) { 
        this.personId = personId; 
        this.credentialId = credentialId; 
    }
    
    public void Customers(Long id, Long personId , Long credentialId) { 
        this.id = id; 
        this.personId = personId; 
        this.credentialId = credentialId; 
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getCredentialId() {
        return credentialId;
    }

    public void setCredentialId(Long credentialId) {
        this.credentialId = credentialId;
    }

    public Long getPersonId() {
        return personId;
    }

    public void setPersonId(Long personId) {
        this.personId = personId;
    }

    public Collection<Cart> getCartCollection() {
        return cartCollection;
    }

    public void setCartCollection(Collection<Cart> cartCollection) {
        this.cartCollection = cartCollection;
    }
    
    public void addCart(Cart c) { 
       cartCollection.add(c);
    }
    
    public void deleteCart(Cart c) { 
       cartCollection.remove(c);
    }
    
    public void addMovie(Cart c, CartItem item) {
        Cart cart = null; 
        Iterator<Cart> it = cartCollection.iterator(); 
        while(it.hasNext()) { 
            if (it.next().getId() == c.getId()) { 
               cart = (Cart)it.next();
               break; 
            } 
        }
        cart.addCartItem(item);
    }
    
    public void deleteMovie(Cart c, CartItem item) {
        Cart cart = null; 
        Iterator<Cart> it = cartCollection.iterator(); 
        while(it.hasNext()) { 
            if (it.next().getId() == c.getId()) { 
               cart = (Cart)it.next();
               break; 
            } 
        }
        cart.deleteCartItem(item);
    }
    
    public int getSize() { 
        return cartCollection.size(); 
    }
        
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Customers other = (Customers) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 41 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
   
    @Override
    public String toString() {
        return "trefliq.persistence.entity.Customers[ id=" + id + " ]";
    }
    
}